//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2004 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using System;
using System.Text;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Tests the handling of a soap request that
	/// has a parameter href.
	/// </summary>
	/// <remarks>
	/// <para>For example:</para>
	/// <para>
	/// <code>
	/// <![CDATA[
	/// <?xml version="1.0" encoding="utf-8"?>
	/// <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:Text" xmlns:types="urn:Text/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	/// 	<soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
	/// 		<q1:CompressList xmlns:q1="java:net.soaptrace.webservices.TextEJB">
	/// 			<string href="#id1" />
	/// 		</q1:CompressList>
	/// 		<soapenc:Array id="id1" soapenc:arrayType="xsd:string[3]">
	/// 			<Item>First</Item>
	/// 			<Item>Second</Item>
	/// 			<Item>Third</Item>
	/// 		</soapenc:Array>
	/// 	</soap:Body>
	/// </soap:Envelope>
	/// ]]>
	/// </code>
	/// </para>
	/// </remarks>
	[ TestFixture ]
	public class SoapRequestWithHrefTestFixture
	{
		/// <summary>
		/// The soap message under test.
		/// </summary>
		SoapRequest m_SoapRequest;
		
		/// <summary>
		/// Sets up the test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void FixtureInit( )
		{
			m_SoapRequest = new SoapRequest( );
			
			byte[ ] Bytes = UnicodeEncoding.UTF8.GetBytes( GetSoapRequest( ) );
			
			for( int i = 0; i < Bytes.Length; ++i )
			{
				bool MessageReceived = m_SoapRequest.AddByte( Bytes[ i ] );
				
				if( i == ( Bytes.Length - 1 ) )
				{
					Assert.IsTrue( MessageReceived, "Should have the complete message now." );
				}
				else
				{
					Assert.IsFalse( MessageReceived, "Should not have a message at byte=" + i.ToString( ) + ", Bytes.Length=" + Bytes.Length );
				}
			}
		}
		
		/// <summary>
		/// Checks that the web service has been extracted
		/// correctly.
		/// </summary>
		[ Test ]
		public void WebService( )
		{
			Assert.AreEqual( "TextEJB", m_SoapRequest.WebService, "Incorrect web service name." );
		}
		
		/// <summary>
		/// Checks that the web operation has been extracted correctly.
		/// </summary>
		[ Test ]
		public void WebOperation( )
		{
			Assert.AreEqual( "CompressList", m_SoapRequest.WebMethod, "Incorrect web operation name." );			
		}
		
		/// <summary>
		/// Gets the soap request xml.
		/// </summary>
		/// <returns>A soap request.</returns>
		private string GetSoapRequest( )
		{
			return "POST /TextEJB.asmx HTTP/1.1\r\n" +
				"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 1.1.4322.573)\r\n" +
				"Content-Type: text/xml; charset=utf-8\r\n" +
				"SOAPAction: \"urn:CompressList\"\r\n" +
				"Content-Length: 660\r\n" +
				"Expect: 100-continue\r\n" +
				"Connection: Keep-Alive\r\n" +
				"Host: localhost:7001\r\n" +
				"\r\n" +
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
				"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 
				"xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" " + 
				"xmlns:tns=\"urn:Text\" xmlns:types=\"urn:Text/encodedTypes\" " + 
				"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
				"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + 
				"<soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + 
				"<q1:CompressList xmlns:q1=\"java:net.soaptrace.webservices.TextEJB\">" + 
				"<string href=\"#id1\" />" + 
				"</q1:CompressList>" + 
				"<soapenc:Array id=\"id1\" soapenc:arrayType=\"xsd:string[3]\">" + 
				"<Item>First</Item>" + 
				"<Item>Second</Item>" + 
				"<Item>Third</Item>" + 
				"</soapenc:Array>" + 
				"</soap:Body>" + 
				"</soap:Envelope>";
		}
	}
}
